package com.weijian.util;

/**
 * Redis键工具类
 *
 * @author weijian
 */
public class RedisKeyUtil {

    /**
     * Redis键分隔符
     */
    private static final String SEPARATOR = ":";

    /**
     * 用户相关键前缀
     */
    private static final String USER_PREFIX = "user";

    /**
     * 会话相关键前缀
     */
    private static final String SESSION_PREFIX = "session";

    /**
     * 登录失败计数键前缀
     */
    private static final String LOGIN_FAIL_PREFIX = "login_fail";

    /**
     * 获取用户信息缓存键
     *
     * @param userId 用户ID
     * @return Redis键
     */
    public static String getUserInfoKey(Long userId) {
        return USER_PREFIX + SEPARATOR + "info" + SEPARATOR + userId;
    }

    /**
     * 获取用户会话键
     *
     * @param sessionId 会话ID
     * @return Redis键
     */
    public static String getUserSessionKey(String sessionId) {
        return SESSION_PREFIX + SEPARATOR + "user" + SEPARATOR + sessionId;
    }

    /**
     * 获取登录失败计数键
     *
     * @param usernameOrEmail 用户名或邮箱
     * @return Redis键
     */
    public static String getLoginFailKey(String usernameOrEmail) {
        return LOGIN_FAIL_PREFIX + SEPARATOR + usernameOrEmail;
    }


//  ==============================课程管理模块=====================================


    /**
     * 课程列表缓存键前缀
     */
    private static final String COURSE_LIST_PREFIX = "course:list";

    /**
     * 课程详情缓存键前缀
     */
    private static final String COURSE_DETAIL_PREFIX = "course:detail";

    /**
     * 获取课程列表缓存键
     *
     * @param page 页码
     * @param size 每页大小
     * @return Redis键
     */
    public static String getCourseListKey(Integer page, Integer size) {
        return COURSE_LIST_PREFIX + SEPARATOR + page + SEPARATOR + size;
    }

    /**
     * 获取课程详情缓存键
     *
     * @param id 课程ID
     * @return Redis键
     */
    public static String getCourseDetailKey(Long id) {
        return COURSE_DETAIL_PREFIX + SEPARATOR + id;
    }


//  ==============================购物车模块=====================================

    /**
     * 购物车缓存键前缀
     */
    private static final String CART_PREFIX = "cart";

    /**
     * 获取购物车缓存键
     *
     * @param userId 用户ID
     * @return Redis键
     */
    public static String getCartKey(Long userId) {
        return CART_PREFIX + SEPARATOR + userId;
    }

    /**
     * 获取购物车商品数量缓存键
     *
     * @param userId 用户ID
     * @return Redis键
     */
    public static String getCartCountKey(Long userId) {
        return CART_PREFIX + SEPARATOR + "count" + SEPARATOR + userId;
    }


//  ==============================订单模块=====================================

    /**
     * 课程库存键前缀
     */
    private static final String COURSE_STOCK_PREFIX = "course:stock";

    /**
     * 订单锁键前缀
     */
    private static final String ORDER_LOCK_PREFIX = "order:lock";

    /**
     * 订单过期键前缀
     */
    private static final String ORDER_EXPIRE_PREFIX = "order:expire";

    /**
     * 获取课程库存缓存键
     *
     * @param courseId 课程ID
     * @return Redis键
     */
    public static String getCourseStockKey(Long courseId) {
        return COURSE_STOCK_PREFIX + SEPARATOR + courseId;
    }

    /**
     * 获取用户订单锁键（防重复提交）
     *
     * @param userId   用户ID
     * @param courseId 课程ID
     * @return Redis键
     */
    public static String getOrderLockKey(Long userId, Long courseId) {
        return ORDER_LOCK_PREFIX + SEPARATOR + "user" + SEPARATOR + userId + SEPARATOR + "course" + SEPARATOR + courseId;
    }

    /**
     * 获取订单过期键
     *
     * @param orderNo 订单号
     * @return Redis键
     */
    public static String getOrderExpireKey(String orderNo) {
        return ORDER_EXPIRE_PREFIX + SEPARATOR + orderNo;
    }


//  ==============================签到模块=====================================

    /**
     * 用户签到键前缀
     */
    private static final String SIGN_IN_PREFIX = "user:signin";

    /**
     * 获取用户签到Bitmap键
     *
     * @param userId 用户ID
     * @param year   年份
     * @return Redis键
     */
    public static String getUserSignInKey(Long userId, int year) {
        return SIGN_IN_PREFIX + SEPARATOR + userId + SEPARATOR + year;
    }

    /**
     * 获取签到统计缓存键
     *
     * @param userId 用户ID
     * @return Redis键
     */
    public static String getSignInStatsKey(Long userId) {
        return SIGN_IN_PREFIX + SEPARATOR + "stats" + SEPARATOR + userId;
    }
}